/*
 * Copyright (c) 2005 -2008 Reliable Source, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Mozilla Public License v.1.1,
 * BSD, and Eclipse Public License v1.0 which accompanies this
 * distribution and is available at:
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 * 		http://www.opensource.org/licenses/bsd-license.php
 * 		http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * Contributors:
 *     IBM Corporation	     -  http://ibm.com
 *     	- initial API and implementation
 *
 *     patientos.org         - http://patientos.org
 *     	- domain and database persistence layer
 *
 *     Reliable Source, Inc. - http://reliablesource.org
 *     	- Symbiosys Application Framework,
 *     	- Symbiosys Application, Symbiosys Product
 *
 */
package org.reliablesource.symbiosys.platform.views.properties.tabbed.patientrecord;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.PropertyDescriptor;

import com.patientis.model.common.IBaseModel;
import com.patientis.model.common.ModelField;

/**
 * 
 * SymbiosysPropertySourceAdapter.java<br>
 * Created on: Apr 25, 2006<br>
 * 
 * @author <a href="mailto:dennis.park@gmail.com">Dennis Park</a>
 * 
 * 
 * @see IPropertySource
 * @since 1.0
 */
public class SymbiosysPropertySourceAdapter implements IPropertySource {
    final protected List<IPropertyDescriptor> descriptorsList;
    final protected List<IPropertyDescriptor> mergedDescriptorsList;
    final private IBaseModel element;

    public SymbiosysPropertySourceAdapter(final IBaseModel _element) {
	super();
	descriptorsList = new ArrayList<IPropertyDescriptor>();
	mergedDescriptorsList = new ArrayList<IPropertyDescriptor>();
	element = _element;
    }

    public Object getEditableValue() {
	return null;
    }

    public IPropertyDescriptor[] getPropertyDescriptors() {
	List<IPropertyDescriptor> _fields = getModelFields(element);
	return _fields.toArray(new IPropertyDescriptor[_fields.size()]);
    }

    private List<IPropertyDescriptor> getModelFields(IBaseModel _base) {
	List<IPropertyDescriptor> _descriptors = new ArrayList<IPropertyDescriptor>();

	Iterator<ModelField> _it = _base.getFields().iterator();
	while (_it.hasNext()) {
	    ModelField _field = _it.next();
	    PropertyDescriptor _pd = new PropertyDescriptor(_field
		    .getFieldName(), _field.getFieldName() + " : ");
	    _descriptors.add(_pd);
	}

	List<IBaseModel> _children = _base.getChildren();
	Iterator<IBaseModel> _childIt = _children.iterator();
	while (_childIt.hasNext())
	    _descriptors.addAll(getModelFields(_childIt.next()));
	return _descriptors;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java
     * .lang.Object)
     */
    public Object getPropertyValue(Object _propertyId) {
	String _returnValue = null;
	Iterator<ModelField> _it = element.getFields().iterator();
	while (_it.hasNext()) {
	    ModelField _field = _it.next();
	    if (_field.getFieldName().equals(_propertyId)) {
		return element.getValue(_field.getModelReference());
	    }
	}
	return _returnValue;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.ui.views.properties.IPropertySource#isPropertySet(java.lang
     * .Object)
     */
    public boolean isPropertySet(Object _id) {
	Object _prop = getPropertyValue(_id);
	if (null == _prop)
	    return false;
	return true;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java
     * .lang.Object)
     */
    public void resetPropertyValue(Object _id) {
	//
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java
     * .lang.Object, java.lang.Object)
     */
    public void setPropertyValue(Object _id, Object _value) {
	//
    }
}
